<template>
  <div>
    <el-button :icon="Plus" type="primary" @click="saveValue">添加SPU</el-button>
    <el-table border style="margin: 20px 0" :data="spuList">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <el-button
            size="small"
            title="添加SKU"
            @click="addSkuData(row)"
            type="primary"
            :icon="Plus"
          ></el-button>
          <el-button
            size="small"
            title="修改SPU"
            type="primary"
            @click="toEditAttr(row)"
            :icon="Edit"
          ></el-button>
          <el-button
            size="small"
            title="查看SKU列表"
            type="info"
            :icon="InfoFilled"
            @click="checkSkuList(row.id)"
          ></el-button>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="真的确定要删除此条数据么?"
            @confirm="deleteSpuInfo(row.id)"
          >
            <template #reference>
              <el-button title="删除" type="danger" size="small" :icon="Delete" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->
    <el-dialog v-model="dialogSkuVisible" title="显示Sku列表">
      <el-table :data="skuList">
        <el-table-column property="skuName" label="名称" width="150" />
        <el-table-column property="price" label="价格" width="200" />
        <el-table-column property="weight" label="重量(千克)" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <el-image style="width: 100px; height: 100px" :src="row.skuDefaultImg" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts">
export default {};
</script>

<script lang="ts" setup>
// 引入接口函数
import { reqGetSpuListData, reqDeleteSpuInfo } from "@/api/product/spu";
import { reqGetSkuListData } from "@/api/product/sku";
import { useCategoryStore } from "@/stores/categoryList";
import { ref, watch } from "vue";
import { ElMessage } from "element-plus";
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
// 接收自定义事件
// 自定义事件
const emits = defineEmits(["changeValue", "getSpuInfo"]);
const saveValue = () => {
  // 切换页面
  emits("changeValue", 2);
};

const small = ref(false);
const background = ref(false);
const disabled = ref(false);
// 存储三级导航的pinia
const categoryStore = useCategoryStore();
// 分页相关的变量
const currentPage = ref<number>(1);
const pageSize = ref<number>(3);
const total = ref<number>(1);
const spuList = ref([]);

// 定义当前组件变量
const isShowSpuInfoList = ref<boolean>(true);

// 对话框显示与否
let dialogSkuVisible = ref<boolean>(false);

// 定义sku列表
const skuList = ref([]);

// 定义请求函数
// 1. 定义一个获取spu列表数据的函数
const getSpuListData = async () => {
  const result = await reqGetSpuListData(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id as number
  );
  spuList.value = result.records;
  total.value = result.total;

  // console.log("SPuList数据", spuList.value);
};

// 分页器
// 分页相关的两个事件
// 3. 更改每页显示条数时触发的事件处理程序
const handleSizeChange = (val: number) => {
  // val就是每页显示的条数 其实也相当于是pageSize
  pageSize.value = val;
  currentPage.value = 1;
  // 重新获取页面列表数据
  getSpuListData();
};

// 4. 根据当前页码进行切换时触发的函数
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`)
  currentPage.value = val;
  getSpuListData();
};

// 删除按钮
// 5. 删除spu
const deleteSpuInfo = async (id: number) => {
  await reqDeleteSpuInfo(id);
  ElMessage.success("删除spu成功...");

  // 重新刷新页面
  getSpuListData();
};

// 点击修改按钮 先实现数据回显
const toEditAttr = (row: any) => {
  emits("changeValue", 2);
  // 将回显所需要的数据进行传递
  emits("getSpuInfo", row);
  // console.log(row);
};

// 点击添加sku按钮事件
const addSkuData = (row: any) => {
  // 跳转到sku添加页面
  emits("changeValue", 3);
  emits("getSpuInfo", row);
};

// 点击查看按钮
const checkSkuList = async (id: number) => {
  dialogSkuVisible.value = true;
  const result = await reqGetSkuListData(id);
  // 存储请求回来的数据
  skuList.value = result;
};

// 添加监视,当三级导航变化时发送请求
watch(
  () => categoryStore.category3Id,
  async (category3Id) => {
    if (!category3Id) {
      spuList.value = []; // 赋值为 空
      return; // 不发送请求
    }
    // 发送请求获取列表数据
    getSpuListData();
  }, //当页面打开就开始监听
  { immediate: true }
);
</script>

<style></style>
